package com.wind.sqlite.common.base;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BaseModelPageDTO<T> extends BasePageDTO {


    protected Integer id;

    protected T data;

    protected List<T> list;

    protected Map<String, Object> search;

    protected String appId;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public Map<String, Object> getSearch() {
        return search;
    }

    public void setSearch(Map<String, Object> search) {
        this.search = search;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public void castDataType(Class<T> clazz) {
        if (data instanceof JSONObject) {
            data = JSON.toJavaObject((JSONObject) data, clazz);
        }
    }

    public void castListType(Class<T> clazz) {
        if (list != null && list.size() > 0) {
            Object firstObj = list.get(0);
            if (firstObj instanceof JSONObject) {
                List<T> objList = new ArrayList<T>(list.size());
                for (T t : list) {
                    t = JSON.toJavaObject((JSONObject) t, clazz);
                    objList.add(t);
                }
                list = objList;
            }
        }
    }
}
